//
//  WorkingCalendarCell.m
//  ProjectSource_Demo
//
//  Created by Liu on 16/3/23.
//  Copyright © 2016年 AngryBear. All rights reserved.
//

#import "WorkingCalendarCell.h"

@implementation WorkingCalendarCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.underView];
        [self.underView addSubview:self.typeImageView];
        [self.underView addSubview:self.typeLabel];
        [self.underView addSubview:self.middleLine];
        [self.underView addSubview:self.titleLabel];
        [self.underView addSubview:self.deadlineLabel];
        [self.underView addSubview:self.dateLabel];
        
        [self setAutoLayout];
        
        [self addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    UIColor *selectedColor = self.selected ? [UIColor colorWithHexRGB:0xf2f2f2] : [UIColor whiteColor];
    self.underView.backgroundColor = selectedColor;
}

- (void)setAutoLayout
{
    [self.underView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@6);
        make.right.mas_equalTo(@(-6));
        make.top.mas_equalTo(@6);
        make.bottom.mas_equalTo(@(0));
    }];
    
    [self.typeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@6);
        make.top.mas_equalTo(@5);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeImageView.mas_right).with.offset(5);
        make.right.mas_equalTo(@(-6));
        make.centerY.equalTo(self.typeImageView);
    }];
    
    [self.middleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@36);
        make.right.mas_equalTo(@(-7));
        make.top.mas_equalTo(@34);
        make.height.mas_equalTo(@(0.5));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@38);
        make.right.equalTo(self.middleLine);
        make.top.mas_equalTo(@42);
    }];
    
    [self.deadlineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.mas_equalTo(@63);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.deadlineLabel.mas_right);
        make.centerY.equalTo(self.deadlineLabel);
        make.right.equalTo(self.titleLabel);
    }];
}

#pragma mark - Getter Methods
- (UIView *)underView
{
    if (_underView == nil) {
        _underView = [[UIView alloc] initWithFrame:CGRectZero];
        _underView.backgroundColor = [UIColor whiteColor];
        _underView.layer.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.21].CGColor;
        _underView.layer.shadowOpacity = 1.0f;
        _underView.layer.shadowOffset = CGSizeMake(0, 1);
        _underView.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:0.21].CGColor;
        _underView.layer.borderWidth = 0.5;
        _underView.layer.masksToBounds = YES;
        _underView.layer.cornerRadius = 2.0f;
        _underView.userInteractionEnabled = NO;
    }
    return _underView;
}

- (UIImageView *)typeImageView
{
    if (_typeImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.backgroundColor = [UIColor clearColor];
        _typeImageView = imageView;
    }
    return _typeImageView;
}

- (UILabel *)typeLabel
{
    if (_typeLabel == nil) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 1;
        label.textColor = [UIColor colorWithHexRGB:0x222222];
        label.font = [UIFont systemFontOfSize:17];
        label.textAlignment = NSTextAlignmentLeft;
        _typeLabel = label;
    }
    return _typeLabel;
}

- (UIView *)middleLine
{
    if (!_middleLine) {
        _middleLine = [[UIView alloc] initWithFrame:CGRectZero];
        _middleLine.backgroundColor = [UIColor colorWithHexRGB:0xf2f2f2];
    }
    return _middleLine;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        label.textColor = [UIColor colorWithHexRGB:0x888888];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentLeft;
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UILabel *)deadlineLabel
{
    if (_deadlineLabel == nil) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        label.textColor = [UIColor colorWithHexRGB:0x888888];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentLeft;
        _deadlineLabel = label;
    }
    return _deadlineLabel;
}

- (UILabel *)dateLabel
{
    if (_dateLabel == nil) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        label.textColor = [UIColor colorWithHexRGB:0x0091cd];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentLeft;
        _dateLabel = label;
    }
    return _dateLabel;
}

@end
